package com.dianrui.moonfire.walkbaby;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


import com.blankj.utilcode.util.SPUtils;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.activity.BaseActivity;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.JsonUtils;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * Description:遛娃借车结束
 * User: lzy
 * Date: 2020-8-20
 */
public class WalkBabyReturnCarActivity extends BaseActivity {
    private String mOrderId;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.order_code)
    TextView orderCode;
    @BindView(R.id.run_times)
    TextView runTimes;
    @BindView(R.id.total)
    TextView total;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.start_path)
    TextView startPath;
    @BindView(R.id.start_time)
    TextView startTime;
    @BindView(R.id.end_path)
    TextView endPath;
    @BindView(R.id.end_time)
    TextView endTime;
    @BindView(R.id.carnumber)
    TextView carnumber;


    @Override
    public int getContentViewId() {
        return R.layout.activity_walkbaby_retuncar;
    }

    @Override
    public void initView() {
        customInit(true);
        title.setText(getString(R.string.returnbabycar_title));
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                mOrderId = bundle.getString("order_id");
            }
        }
        getCarInfo();
    }

    @Override
    public void initData() {

    }

    public void getCarInfo() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        if (!StringUtils.isEmpty(mOrderId)) {
            jsonObject.addProperty("order_id", mOrderId);
        }
        try {
            XHttpRequest.getInstance().postRequests(Url.BABYORDEREND, jsonObject.toString(), new XHttpRequest.XCallBack() {
                @Override
                public void onResponse(String result) throws JSONException {
                    if (!StringUtils.isEmpty(result)) {
                            final JSONObject jsonObject = new JSONObject(result);
                            if (null != jsonObject && !isFinishing()) {
                                if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                    String data = JsonUtils.getJsonStr(result, "data");
                                    orderCode.setText(JsonUtils.getJsonStr(data,"order_code"));
                                    runTimes.setText(JsonUtils.getJsonStr(data,"run_times"));

                                    StringBuilder totalBulider=new StringBuilder();
                                    totalBulider.append(JsonUtils.getJsonStr(data,"total")).append("元");
                                    total.setText(totalBulider.toString());

                                    StringBuilder moneyBulider=new StringBuilder();
                                    moneyBulider.append(JsonUtils.getJsonStr(data,"money")).append("元");
                                    money.setText(moneyBulider.toString());

                                    money.setText(moneyBulider.toString());
                                    startPath.setText(JsonUtils.getJsonStr(data,"start_path"));
                                    endPath.setText(JsonUtils.getJsonStr(data,"end_path"));
                                    startTime.setText(JsonUtils.getJsonStr(data,"start_time"));
                                    endTime.setText(JsonUtils.getJsonStr(data,"end_time"));
                                    carnumber.setText(JsonUtils.getJsonStr(data,"number"));
                                }
                            }
                    }
                }

                @Override
                public void failedRequest(String result) {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            });
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.back, R.id.copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.copy:
                copyNumber();
                break;
            default:
                break;
        }
    }

    //复制车辆编码
    private void copyNumber() {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
        ClipData clipData = ClipData.newPlainText(null, carnumber.getText().toString());
        // 把数据集设置（复制）到剪贴板
        if (clipboard != null) {
            clipboard.setPrimaryClip(clipData);
        }
        ToastUtil.showToast("复制成功");
    }
}
